package com.jiuxian.scheduling.rpc.rpc;

import com.jiuxian.scheduling.rpc.RequestRpc;
import com.jiuxian.scheduling.util.HttpUtil;
import com.jiuxian.scheduling.util.SiteUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangjingqing on 2017/12/18.
 */
public class HttpRequestRpc extends RequestRpc {

    Logger logger = LoggerFactory.getLogger(DubboRequestRpc.class);
    /** 请求路径 */
    private String path;

    /** 请求方式 get、post */
    private String method;

    /** 请求类型 text、json */
    private String contentType;

    /**  请求参数*/
    private String parameters;

    @Override
    public Object request() {
        try {
            if("get".equalsIgnoreCase(method)){
                return HttpUtil.get(path);
            }else if("post".equalsIgnoreCase(method) && StringUtils.isBlank(contentType)){
                return HttpUtil.post(path, SiteUtil.getMapForJSON(parameters));
            }else if("post".equalsIgnoreCase(method) && "json".equalsIgnoreCase(contentType)){
                return HttpUtil.postJSON(path,parameters);
            }else{
                throw new RuntimeException("不支持http的"+method+"-"+contentType+"请求");
            }
        } catch (Exception e) {
            logger.error("请求异常"+path+"-"+method,e);
        }
        return null;
    }



    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }
}
